﻿using Bridge.Shared.Models;
using Mapster;
using Microsoft.EntityFrameworkCore;
using OrderVouchers;
using OrderVouchers.Input;
using OrderVouchers.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;

namespace Vouchers
{
    public class OrderVoucherService : ApplicationService, IOrderVoucherService
    {
        private readonly IRepository<OrderVoucher, Guid> _orderRepository;
        public OrderVoucherService(IRepository<OrderVoucher, Guid> orderRepository) {
        _orderRepository = orderRepository;
        }
        public async Task<PageResponseResult<OrderVoucherOutput>> GetOrderList(OrderVoucherInput input)
        {
            var query = (await _orderRepository.GetQueryableAsync()).WhereIf(!string.IsNullOrEmpty(input.VoucherCode), a => a.VoucherCode.Contains(input.VoucherCode));

            var totalCount = query.Count(); 
            var list = await query.Skip(input.PageSize * input.PageIndex).Take(input.PageSize).ToListAsync();
            var dto = list.Adapt<List<OrderVoucherOutput>>();
       
            return PageResponseResult<OrderVoucherOutput>.Success("ok", dto, input.PageSize, input.PageIndex, totalCount);
        }

        public async Task<ResponseResult<OrderVoucherOutput>> GetOrderInfo(Guid id)
        {
            var order = await _orderRepository.FirstOrDefaultAsync(a => a.Id == id);
            if (order == null)
            {
                return ResponseResult<OrderVoucherOutput>.Fail("记录不存在！");
            }

            var dto = order.Adapt<OrderVoucherOutput>();
            return ResponseResult<OrderVoucherOutput>.Success("ok", dto);
        }

        public async Task<ResponseResult> CreateOrUpdateOrder(CreateOrUpdateOrderVoucherInput input)
        {
            try
            {
                if (input == null)
                {
                    return ResponseResult.Fail("参数不能为空！");
                }
                if (input.Id.HasValue)
                {
                    var order = await _orderRepository.FirstOrDefaultAsync(_ => _.Id == input.Id);
                    if (order == null)
                    {
                        return ResponseResult.Fail("单据不存在！");
                    }

                    order.SupplierId = input.SupplierId;
                    order.SupplierName = input.SupplierName;
                    order.WarehouseId = input.WarehouseId;
                    order.WarehouseName = input.WarehouseName;
                    await _orderRepository.UpdateAsync(order);
                }
                else
                {
                    OrderVoucher order = new OrderVoucher(GuidGenerator.Create())
                    {
                        VoucherCode = input.VoucherCode,
                        SupplierId = input.SupplierId,
                        SupplierName = input.SupplierName,
                        WarehouseId = input.WarehouseId,
                        WarehouseName = input.WarehouseName,
                        Status = input.Status

                    };
                    await _orderRepository.InsertAsync(order);
                }
                return ResponseResult.Success("ok");
            }
            catch (Exception e)
            {

                return ResponseResult.Fail($"操作失败，原因：{e.Message}");
            }

            
        }
    }
}
